{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "04afd232",
   "metadata": {},
   "source": [
    "## 人脸识别实践---宿舍智能识别门禁系统\n",
    "\n",
    "* 1. 创建FaceSet\n",
    "* 2. 查询FaceSet\n",
    "* 3. 存入face_token数据 \n",
    "* 4. 获取FaceSet信息数据\n",
    "* 5. 人脸搜索\n",
    "* 6. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c74c4c2",
   "metadata": {},
   "source": [
    "# 创建FaceSet\n",
    "\n",
    "* 描述\n",
    "* 1.创建一个人脸的集合FaceSet，用于储存人脸标识face_token。一个FaceSet能储存10000个face_token。\n",
    "* 2. 试用API key可以创建1000个FaceSet，正式API key可以创建10000个FaceSet\n",
    "\n",
    "* 调用url\n",
    ">\"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "* 调用方法\n",
    "> POST\n",
    "\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2691e5c9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T10:59:53.668681Z",
     "start_time": "2022-04-04T10:59:53.497263Z"
    }
   },
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "dfbfca61",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T10:59:55.218979Z",
     "start_time": "2022-04-04T10:59:55.211965Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "71863175",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:00:20.189917Z",
     "start_time": "2022-04-04T11:00:19.428570Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "FaceSet_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "#4. 准备playload(酬载)\n",
    "payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_sercret,  \n",
    "}\n",
    "\n",
    "# 4. 发起请求\n",
    "\n",
    "r = requests.post(FaceSet_url,payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "56421d90",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:00:20.987564Z",
     "start_time": "2022-04-04T11:00:20.974473Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2d6d7beb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-03-28T12:11:34.804052Z",
     "start_time": "2022-03-28T12:11:34.786386Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6d33577177a08fe496a314c37ffd79a0',\n",
       " 'time_used': 154,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648469459,76b1e26e-c238-4c16-8305-71d0914796ae',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1f518222",
   "metadata": {},
   "source": [
    "# 查询faceset\n",
    "\n",
    "* 描述\n",
    "* 1.获取一个 FaceSet 的所有信息，包括此 FaceSet 的 faceset_token, outer_id, display_name 的信息，以及此 FaceSet 中存放的 face_token 数量与列表。\n",
    "* 2.注意：2017年8月16日后，调用本接口将不会一次性返回全部的 face_token 列表。单次查询最多返回 100 个 face_token。如需获取全量数据，需要配合使用 start 和 next 参数。请尽快修改调整您的程序\n",
    "\n",
    "* 调用url\n",
    "https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\n",
    "\n",
    "* 调用方法\n",
    "**POST**\n",
    "\n",
    "* 调用参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ac14562d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:01:09.105675Z",
     "start_time": "2022-04-04T11:01:08.560005Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "FaceSet_getfacesets_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets'\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "#4. 准备playload(酬载)\n",
    "payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_sercret,\n",
    "#     'image_url': 'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "# 4. 发起请求\n",
    "\n",
    "r1 = requests.post(url = FaceSet_getfacesets_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "b7a459d6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:01:10.150462Z",
     "start_time": "2022-04-04T11:01:10.142430Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "9e7051cf",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:01:10.839702Z",
     "start_time": "2022-04-04T11:01:10.818694Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 83,\n",
       " 'facesets': [{'faceset_token': '2c0b87758cbeaaa5f67151fb959f9c7c',\n",
       "   'outer_id': 'NFUSDD001',\n",
       "   'display_name': 'nf_university_1栋',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'e1ce6d113dfe04af3d7fc0eadb49bdb9',\n",
       "   'outer_id': 'NFUSD001',\n",
       "   'display_name': 'nf_university_1栋',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '6d33577177a08fe496a314c37ffd79a0',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'e3dc4c12dfa6fe2a58c54a530397956d',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1649070069,fd3055a4-4263-4779-9d9c-ac4a44eb1fed'}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r1.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d7383456",
   "metadata": {},
   "source": [
    "# 更新人脸库信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f21c170c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:06:01.491785Z",
     "start_time": "2022-04-04T11:06:00.356275Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1.url\n",
    "FaceSet_update_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/update'\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "#4. 准备playload(酬载)\n",
    "payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_sercret,\n",
    "    'faceset_token': '6d33577177a08fe496a314c37ffd79a0',\n",
    "    'display_name':'nf_university_1栋',\n",
    "    'new_outer_id':'NFUSDD002',\n",
    "    'user_data':'6层，满员，女生宿舍，460位同学，均已录入face_token',\n",
    "}\n",
    "\n",
    "# 4. 发起请求\n",
    "\n",
    "r2 = requests.post(url = FaceSet_update_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "fea0c998",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:06:01.521816Z",
     "start_time": "2022-04-04T11:06:01.510814Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "5d7edf74",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:06:03.835919Z",
     "start_time": "2022-04-04T11:06:03.828892Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6d33577177a08fe496a314c37ffd79a0',\n",
       " 'request_id': '1649070361,7e2951c3-8823-4520-8f03-03710e33d199',\n",
       " 'time_used': 77,\n",
       " 'outer_id': 'NFUSDD002'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r2.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "97a4c436",
   "metadata": {},
   "source": [
    "# 检查数据集合信息"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "8705a995",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:07:54.924139Z",
     "start_time": "2022-04-04T11:07:54.170775Z"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "# 1.url\n",
    "FaceSet_update_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail'\n",
    "\n",
    "# 2.API账户/通行证\n",
    "API_key = 'UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P'\n",
    "API_sercret = 'uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "#4. 准备playload(酬载)\n",
    "payload = {\n",
    "    'api_key': API_key,\n",
    "    'api_secret': API_sercret,\n",
    "    'faceset_token': '2c0b87758cbeaaa5f67151fb959f9c7c',\n",
    "#     'outer_id':'NFUSD001',\n",
    "}\n",
    "\n",
    "# 4. 发起请求\n",
    "\n",
    "r3 = requests.post(url = FaceSet_update_url, params = payload)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "aa5f0a83",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:08:26.163458Z",
     "start_time": "2022-04-04T11:08:26.145325Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "a5599443",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:07:55.081859Z",
     "start_time": "2022-04-04T11:07:55.060352Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '2c0b87758cbeaaa5f67151fb959f9c7c',\n",
       " 'tags': '',\n",
       " 'time_used': 84,\n",
       " 'user_data': '6层，满员，女生宿舍，460位同学，均已录入face_token',\n",
       " 'display_name': 'nf_university_1栋',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1649070475,35d98dd4-1d60-4473-956b-def755617a7b',\n",
       " 'outer_id': 'NFUSDD001'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r3.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0eb6b3b5",
   "metadata": {},
   "source": [
    "# 人脸检测，获取人脸token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "c2e39d89",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:10:50.606179Z",
     "start_time": "2022-04-04T11:10:50.559563Z"
    }
   },
   "outputs": [],
   "source": [
    "import detect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "8730f24e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:10:53.300353Z",
     "start_time": "2022-04-04T11:10:53.282264Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module detect:\n",
      "\n",
      "NAME\n",
      "    detect\n",
      "\n",
      "FUNCTIONS\n",
      "    face_detect(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    d:\\桌面\\学习总结\\大二下半学期\\api\\api-course-notes\\week_04\\detect.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(detect)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "09128f3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "detect.face_detect(API_key,API_sercret,#'路径（相对路径）')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "149d746f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2022-04-04T11:11:44.585188Z",
     "start_time": "2022-04-04T11:11:43.910655Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1649070705,5ec194b6-66d0-4078-9e8f-0f2b8f6e51e4',\n",
       " 'time_used': 93,\n",
       " 'faces': [{'face_token': 'da3e17bb772e6dbe1cf029709db2abe9',\n",
       "   'face_rectangle': {'top': 98, 'left': 164, 'width': 118, 'height': 118}}],\n",
       " 'image_id': 'fwD9nHfTb9s3JIGwQOQmSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect.face_detect('UwBYz5inBNZ0c3vErNAzGzZQPokgZo1P','uxgs7RqSD5HCXaNAgyCDaAGO_3_tEJzv','Liu_01.jpeg')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37490097",
   "metadata": {},
   "source": [
    "# 第四周作业：\n",
    "* 尝试创建detect.py 并实现功能调用\n",
    "* 尝试创建analyze.py\\campare.py\\search.py，并实现功能调用\n",
    "* 尝试存放宿舍4位同学的人脸信息到 人脸数据集（XXX栋XXX宿舍）:\n",
    "* 完善faceset信息\n",
    "* 实现人脸搜索和人脸对比，打印结果：是本人 OR 不是本人"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a31a6e1b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
